package com.six.work.controller;

import com.six.work.entity.Announcement;
import com.six.work.entity.AnnouncementView;
import com.six.work.services.AnnouncementServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementServices announcementServices;

    @GetMapping("/list")
    public List<AnnouncementView> getAllAnnouncement(@RequestParam(value="announcementTitle",defaultValue = "") String announcementTitle,@RequestParam(value="admin_id",defaultValue = "0") Integer admin_id){
        List<AnnouncementView> list = null;
        if(admin_id == 0) {
            list = announcementServices.findAnnouncementByName(announcementTitle);
        }else{
            list = announcementServices.findAnnByadIDAndannName(announcementTitle,admin_id);
        }
        return list;
    }

    @GetMapping("/del")
    public Integer deleteByID(@RequestParam("announcement_id") Integer announcement_id){
        return announcementServices.DelAnnouncementByID(announcement_id);
    }

    @PostMapping("/add")
    public Integer InsertAnnouncement(@RequestBody Announcement announcement){

        return announcementServices.insertAnnouncement(announcement);
    }

    @GetMapping("/update")
    public Announcement getUpdatePage(@RequestParam("announcement_id") Integer announcement_id){

        return announcementServices.SelectAnnouncementByID(announcement_id);
    }

    @PostMapping("/update")
    public Integer UpdateAnnouncement(@RequestBody Announcement announcement) {
        return announcementServices.updateAnnouncement(announcement);
    }

}
